﻿Imports System.Data.SqlClient

Public Class frmEmpresas
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal

    Private Sub frmEmpresas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        frmMain.Timer1.Enabled = False


        dgEmpresas.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)
        Usuario.Ubicacion("Empresas")
        CargaEmpresas()

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Usuario.Ubicacion("Ventana principal")
        Close()
    End Sub
    Sub CargaEmpresas()
        dgEmpresas.Rows.Clear()
        Dim cnConn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String

        Try
            cnConn.ConnectionString = InitialConnection
            If UCase(UsuarioActivo) = "ADMINISTRADOR" Then
                strSql = "Select * from Empresas Order by Nombre"
            Else
                Dim IdUsuario As Integer = BuscarIntDatoInicial("SELECT IdUsuario FROM Usuarios WHERE Nombre='" & UsuarioActivo & "'")
                strSql = "Select * from Empresas WHERE IdEmpresa in (SELECT IdEmpresa FROM EmpresasUsuarios WHERE IdUsuario=" & IdUsuario & ") Order by Nombre"
            End If
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql

            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim Cont As Integer = 0

            Do While rdBuscar.Read()
                Cont = Cont + 1
                dgEmpresas.Rows.Add(rdBuscar("IdEmpresa"), rdBuscar("Nombre").ToString.Trim, rdBuscar("BD").ToString.Trim)
            Loop

            If Cont = 0 Then
                MsgBox("No tienes permiso para ninguna empresa", MsgBoxStyle.Exclamation, "Aviso")
                Close()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub

    Private Sub NuevaEmpresaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NuevaEmpresaToolStripMenuItem.Click
        frmNuevaEmpresa.ShowDialog()
    End Sub

    Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click

        Dim CitraRuta As String = dgEmpresas.CurrentRow.Cells(2).Value.ToString.Trim

        Contabilidad = BuscarStrDatoInicial("SELECT Contabilidad FROM Empresas WHERE IdEmpresa=" & dgEmpresas.CurrentRow.Cells(0).Value)
        Nominas = BuscarStrDatoInicial("SELECT Nominas FROM Empresas WHERE IdEmpresa=" & dgEmpresas.CurrentRow.Cells(0).Value)
        Comercial = BuscarStrDatoInicial("SELECT AdminPAQ FROM Empresas WHERE IdEmpresa=" & dgEmpresas.CurrentRow.Cells(0).Value)

        Dim InterfazComercial As String = BuscarStrDatoInicial("SELECT InterfazComercial FROM Empresas WHERE IdEmpresa=" & dgEmpresas.CurrentRow.Cells(0).Value)
        If InterfazComercial = "A" Then
            Usa_AdminPAQ = True
        Else
            Usa_AdminPAQ = False
        End If

        CITRA = CitraRuta

        If Internet = True Then
            CitraConnection = "data source = 201.163.91.243; initial catalog =" & CitraRuta & "; user id = SA; password = Ausl4321OPTRA "
            ContabilidadConnection = "data source = 201.163.91.243\COMPAC; initial catalog =" & Contabilidad & "; user id = SA; password = Ausl4321OPTRA "
            NominasConnection = "data source = 201.163.91.243\COMPAC; initial catalog =" & Nominas & "; user id = SA; password = Ausl4321OPTRA "
            ComercialConnection = "data source =201.163.91.243\COMPAC; initial catalog =" & Comercial & "; user id = sa; password = Ausl4321OPTRA "

            ComercialConnectionInicial = "data source =201.163.91.243\COMPAC; initial catalog =CompacWAdmin; user id = sa; password = Ausl4321OPTRA "
            'MsgBox("La interfaz con AdminPAQ se deshabilita al conectar el sistema a travéz de Internet", MsgBoxStyle.Information, "Aviso")

            'AdminPAQCmnonnection1 = "Driver={Microsoft Visual FoxPro Driver};sourcedb=\\aussvr2\CONTPAQ\CompacW\Empresas\adm211014Ausl;sourcetype=DBF;exclusive=No;"
            'AdminPAQAnteriorConnection = "Driver={Microsoft Visual FoxPro Driver};sourcedb=\\" & Server.Trim & "\CONTPAQ\CompacW\Empresas\MGW2010AUSL;sourcetype=DBF;exclusive=No;"
            'OptocontrolConnection = "Server=DESPACHOOPTOCON\OPTOCONTROL;Database=DFS;User Id=DFS; PassWord=stnsnlbr!QNltcsttxndnHMBR1892;" 'DELDA
            'OptocontrolConnection = "Server=" & Server.Trim & "\COMPAC;Database=OPTOCONTROL;User Id=sa;PassWord=Compac123;" '"Server=MONITOREOPC\OPTOCONTROL;Database=DFS;User Id=DFS; PassWord=stnsnlbr!QNltcsttxndnHMBR1892;" 'TECNOLOGIAS
            'EmpresaActiva = dgEmpresas.CurrentRow.Cells(1).Value
            'AUSLConnection = "Server=" & Server.Trim & "\COMPAC;Database=ctCITRAAUSL2012; user id = SA; password = Compac123;"
            BiometriaConnectionOperadores = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\aussvr2\CONTPAQ\Citra\CITRAAUSL2012\UFDatabase.mdb;"
        Else
            ' InitialConnection = "data source =192.168.254.246; initial catalog = citraGenerales; user id = SA; password = Ausl4321OPTRA "
            ComercialConnectionInicial = "data source = 192.168.254.246\COMPAC; initial catalog =CompacWAdmin; user id = sa; password = Ausl4321OPTRA "
            CitraConnection = "data source = 192.168.254.246; initial catalog =" & CitraRuta & "; user id = sa; password = Ausl4321OPTRA "
            ContabilidadConnection = "data source = 192.168.254.246\COMPAC; initial catalog =" & Contabilidad & "; user id = sa; password = Ausl4321OPTRA "
            NominasConnection = "data source = 192.168.254.246\COMPAC; initial catalog =" & Nominas & "; user id = sa; password = Ausl4321OPTRA "
            AdminPAQConnection1 = "Driver={Microsoft Visual FoxPro Driver};sourcedb=" & Comercial & ";sourcetype=DBF;exclusive=No;"
            AdminPAQAnteriorConnection = "Driver={Microsoft Visual FoxPro Driver};sourcedb=\\" & Server.Trim & "\CONTPAQ\CompacW\Empresas\MGW2010AUSL;sourcetype=DBF;exclusive=No;"
            ComercialConnection = "data source = 192.168.254.246\COMPAC; initial catalog =" & Comercial & "; user id = sa; password = Ausl4321OPTRA "
            'OptocontrolConnection = "Server=DESPACHOOPTOCON\OPTOCONTROL;Database=DFS;User Id=DFS; PassWord=stnsnlbr!QNltcsttxndnHMBR1892;" 'DELDA
            OptocontrolConnection = "data source = 192.168.254.246; initial catalog =OPTOCONTROL;User Id=sa;PassWord=Ausl4321OPTRA;" '"Server=MONITOREOPC\OPTOCONTROL;Database=DFS;User Id=DFS; PassWord=stnsnlbr!QNltcsttxndnHMBR1892;" 'TECNOLOGIAS
            EmpresaActiva = dgEmpresas.CurrentRow.Cells(1).Value
            AUSLConnection = "data source = 192.168.254.246; initial catalog =ctCITRAAUSL2012; user id = sa; password = Ausl4321OPTRA "

            BiometriaConnectionOperadores = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Conopsvr\Citra\UFDatabase.mdb;"

        End If


        frmMain.CatálogosToolStripMenuItem.Visible = True
        frmMain.ModulosToolStripMenuItem.Visible = True
        frmMain.UtileriasToolStripMenuItem.Visible = True

        Bitacora.RegistraActividad("Ingreso a la empresa " & dgEmpresas.CurrentRow.Cells(1).Value.ToString.Trim)
        Usuario.Ubicacion("Ventana principal")

        ' frmMain.Timer1.Enabled = False

        frmMain.statusEmpresa.Text = dgEmpresas.CurrentRow.Cells(1).Value.ToString.Trim
        frmMain.statusActividad.Text = ""
        frmMain.ToolStrip1.Visible = True

        gAliasBDD = Contabilidad 'Empresa de bancos

        Close()

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Dim TotalUsuarios As Integer = BuscarIntDatoInicial("Select Count(*) FROM UsuariosConectados")
        If TotalUsuarios > 1 Then
            MsgBox("El respaldo de la base de datos es un proceso exclusivo que requiere que no existan usuarios conectados ademas del usuario que hara el respaldo", MsgBoxStyle.Exclamation, "Aviso")
        Else
            frmRespaldar.ShowDialog()
        End If
    End Sub

    Private Sub dgEmpresas_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgEmpresas.CellContentClick

    End Sub
    Private Sub dgEmpresas_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgEmpresas.DoubleClick
        ToolStripButton3_Click(Me, System.EventArgs.Empty)
    End Sub
End Class